//
//  RootViewController.m
//  UI_Homewor_13
//
//  Created by 黄威 on 16/5/19.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "RootViewController.h"
#import "ImageCell.h"
#import "ContectCell.h"
@interface RootViewController ()
@property (nonatomic,retain)NSMutableArray *arr;
@property (nonatomic,retain)NSMutableDictionary *dic;
@property (nonatomic,retain)NSMutableArray *ARRAY;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.title = @"🐰";
    
    [self handData];
//    NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    
  //  NSLog(@"%@",paths);
    
    
    [self.tableView registerClass:[ContectCell class] forCellReuseIdentifier:@"pool2"];
    [self.tableView registerClass:[ImageCell class] forCellReuseIdentifier:@"pool"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


}


- (void)handData{

   
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"news.plist"];
    
    self.ARRAY = [NSMutableArray array];// 使用前开辟信息;
    self.arr = [NSMutableArray arrayWithContentsOfFile:path];
    
    
    NSLog(@"%@",path);
    NSLog(@"%@",self.arr);
    
    for (NSDictionary *dic in self.arr) {
        Model *model = [[Model alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.ARRAY addObject:model];
        [model release];
        
    }
    
    NSLog(@"%@",self.ARRAY);
    
}




//    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Contact.plist"];
//    self.dic = [NSMutableDictionary dictionaryWithContentsOfFile:path];



-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    [self.tableView reloadData];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    
    if ([[dic objectForKey:@"type"] isEqualToString:@"1"])
    {
        ContectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2" forIndexPath:indexPath];
    
        cell.model = [self.ARRAY objectAtIndex:indexPath.row];
        
        return cell;
    }else
    
    { ImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
        
        cell.model = [self.ARRAY objectAtIndex:indexPath.row];
        
    return cell;}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    
    if ([[dic objectForKey:@"type"] isEqualToString:@"2"])
       
    return [ImageCell HeighForImage:[UIImage imageNamed:[dic objectForKey:@"image"]] width:[UIScreen mainScreen].bounds.size.width];
        
    else if ([[dic objectForKey:@"type"] isEqualToString:@"1"]) {
    
   
        return [ContectCell HeigtForContect:[dic objectForKey:@"summary"]];
    }
    return 0;
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
